package com.example.template.commontable.service.impl;


import com.example.template.commontable.entity.TableColumn;
import com.example.template.commontable.mapper.TableColumnMapper;
import com.example.template.core.service.impl.MyBatisBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author meko .
 * @date 2019/11/28 10-11-00.
 */
@Slf4j
@Service
public class TableColumnServiceImpl extends MyBatisBaseServiceImpl<TableColumn> {

    @Resource
    private TableColumnMapper tableColumnMapper;

    public List<TableColumn> getEntityByTableName(String tableName) {
        TableColumn searchModel = new TableColumn();
        searchModel.setTableName(tableName);
        List<TableColumn> tableColumnList = tableColumnMapper.select(searchModel);
        log.info("表字段数为：           " + tableColumnList.size());
        if (tableColumnList.size() > 0) {

            return tableColumnList;
        } else {
            log.info("生成表头数据");
            List<TableColumn> columnList = tableColumnMapper.getEntityByTableName(tableName);
            int j = 1000;
            for (TableColumn tableColumn : columnList) {
                tableColumn.setProp(StringUtil.underlineToCamelhump(tableColumn.getProp()));
                if (tableColumn.getParam10() == null) {
                    tableColumn.setParam10("1");
                }
                if (tableColumn.getWidth() == null) {
                    tableColumn.setWidth("auto");
                }
                if (tableColumn.getParam9() == null) {
                    tableColumn.setParam9("false");
                }
                tableColumn.setSort((j + 10) + "");
                j += 10;
            }
            tableColumnMapper.myInsertList(columnList);
            return tableColumnMapper.select(searchModel);
        }

    }
}
